<template>
  <div class="user-container">
    <!-- 用户基本信息面板 -->
    <div class="user-card">
      <!-- 用户头像、姓名 -->
      <van-cell>
        <!-- 使用 title 插槽来自定义标题 -->
        <template #icon>
          <img :src="$store.state.userPhoto" alt="头像" class="avatar" />
        </template>
        <template #title>
          <span class="username">{{ $store.state.userName }}</span>
        </template>
        <template #label>
          <van-tag color="#fff" text-color="#007bff">申请认证</van-tag>
        </template>
      </van-cell>
      <!-- 动态、关注、粉丝 -->
      <div class="user-data">
        <div class="user-data-item">
          <span>{{ userInfoObj?.art_count }}</span>
          <span>动态</span>
        </div>
        <div class="user-data-item">
          <span>{{ userInfoObj?.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="user-data-item">
          <span>{{ userInfoObj?.fans_count }}</span>
          <span>粉丝</span>
        </div>
      </div>
    </div>

    <!-- 操作面板 -->
    <van-cell-group class="action-card">
      <van-cell icon="edit" title="编辑资料" is-link @click="editUserInfo" />
      <van-cell
        icon="chat-o"
        title="小思同学"
        is-link
        @click="$router.push('/Chat')"
      />
      <van-cell icon="warning-o" title="退出登录" is-link @click="quitFn" />
    </van-cell-group>
  </div>
</template>

<script>
import { removeToken } from '@/utils/token'
import { userInfoAPI } from '@/api/index'
import { Dialog, Toast } from 'vant'
import { mapMutations } from 'vuex'
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'user',
  data() {
    return {
      userInfoObj: {},
    }
  },
  created() {
    this.getUserInfo()
  },
  // 使用缓存后，created上生命周期不执行，换成特有的页面激活的activaed生命周期重新发请求
  activated() {
    this.getUserInfo()
  },
  computed: {},
  methods: {
    ...mapMutations(['SET_USERPHOTO', 'SET_USERNAME']),
    removeToken,
    // 1.0  获取用户信息
    async getUserInfo() {
      await userInfoAPI().then((res) => {
        if (res.status !== 200) return Toast.fail('获取用户信息失败')
        this.userInfoObj = res.data.data || {}
        this.SET_USERPHOTO(this.userInfoObj.photo)
        this.SET_USERNAME(this.userInfoObj.name)
      })
    },
    // 2.0 退出
    quitFn() {
      Dialog.confirm({
        title: '登出',
        message: '小主~~ 确认离开我吗 ~~',
      })
        .then(() => {
          // 登出
          this.removeToken()
          this.$router.replace('/Login')
        })
        .catch(() => {})
    },
    // 3.0 修改用户登录页面
    editUserInfo() {
      this.$router.push('/userEdit')
    },
  },
}
</script>

<style scoped lang="less">
.user-container {
  .user-card {
    background-color: #007bff;
    color: white;
    padding-top: 20px;
    .van-cell {
      background: #007bff;
      color: white;
      &::after {
        display: none;
      }
      .avatar {
        width: 60px;
        height: 60px;
        background-color: #fff;
        border-radius: 50%;
        margin-right: 10px;
      }
      .username {
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
  .user-data {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    font-size: 14px;
    padding: 30px 0;
    .user-data-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 33.33%;
    }
  }
}
</style>
